const AblumsService = require("../sercice/AblumsServure")
const format = require("../utils/format")
const AblumsControl = {
    img_many: async (req, res) => {
        try {
            let imgs = []
            req.files.forEach(file => {
                let normalizedPath = file.path.replace(/\\/g, '/');
                let relativePath = normalizedPath.split('public')[1];
                imgs.unshift(relativePath)
            });
            console.log(imgs)
            // 1713686580000   1683532980000 Date.now()
            const isCreact = format.formatDate2(Date.now())
            const result = await AblumsService.find_day(isCreact)
            if (result === null) {
                await AblumsService.img_add(isCreact, imgs)
                res.send({
                    msg: "ok",
                    code: 200
                })
            } else {
                await AblumsService.updata_img(isCreact, imgs)
                res.status(200).send({
                    msg: "ok",
                    code: 200
                })
            }

        } catch (error) {
            res.status(411).send({
                msg: "ok",
                code: 411
            })
        }
    },

    find_imgs: async (req, res) => {
        try {
            console.log(req.params, 'sss')
            const result = await AblumsService.find_imgs(req.params)
            res.status(200).send({
                code: 200,
                data: result
            })
        } catch (error) {
            res.status(411).send({
                code: 411,
                data: ""
            })
        }
    },
    dele_img: async (req, res) => {
        try {
            const result = await AblumsService.delete_img({
                year: req.params.year,
                month: req.params.month,
                day: req.params.day,
                img: req.body.img,

            })
            if (result.matchedCount == 1) {
                res.status(200).send({
                    msg: "删除成功",
                    code: 200
                })
            } else {
                res.status(411).send({
                    msg: "删除失败",
                    code: 411
                })
            }

        } catch (error) {
            res.status(411).send({
                msg: "删除失败",
                cdoe: 411
            })
        }

    }
}
module.exports = AblumsControl